﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace OperaThumbnailer
{
	public class OperaInstallsData
	{
		private List<OperaInstallation> installations;
		public List<OperaInstallation> Installations { get { return installations; } }
		
		public OperaInstallsData()
		{
			installations = new List<OperaInstallation>();
			
			DirectoryInfo execFolder = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath));
			if (File.Exists(execFolder.FullName + @"\opera.exe") &&
			    File.Exists(execFolder.FullName + @"\profile\opthumb.dat") &&
			    Directory.Exists(execFolder.FullName + @"\profile\thumbnails")) {
				OperaInstallation portableInstall = new OperaInstallation();
				portableInstall.Name = "Opera@USB";
				portableInstall.InstallPath = execFolder.FullName;
				portableInstall.ThumbnailDir = execFolder.FullName + @"\profile\thumbnails";
				portableInstall.ThumbDatPath = execFolder.FullName + @"\profile\opthumb.dat";
				installations.Add(portableInstall);
			}
			
			DirectoryInfo programFiles = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
			DirectoryInfo localData = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
			DirectoryInfo roamingData = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
			
			foreach (DirectoryInfo operaDir in programFiles.GetDirectories("*opera*")) {
				if (Directory.Exists(localData.FullName + @"\Opera\" + operaDir.Name)
				    && Directory.Exists(roamingData.FullName + @"\Opera\" + operaDir.Name)) {
					OperaInstallation install = new OperaInstallation();
					install.Name = operaDir.Name;
					install.InstallPath = operaDir.FullName;
					install.ThumbnailDir = localData.FullName + @"\Opera\" + operaDir.Name + @"\thumbnails";
					install.ThumbDatPath = roamingData.FullName + @"\Opera\" + operaDir.Name + @"\opthumb.dat";
					installations.Add(install);
				}
			}
		}
	}
	
	public class OperaInstallation
	{
		private string name;
		private string installPath;
		private string thumbnailDir;
		private string thumbDatPath;
		public string Name { get { return name; } set { name = value; } }
		public string InstallPath { get { return installPath; } set { installPath = value; } }
		public string ThumbnailDir { get { return thumbnailDir; } set { thumbnailDir = value; } }
		public string ThumbDatPath { get { return thumbDatPath; } set { thumbDatPath = value; } }
		
		public OperaInstallation()
		{
			name = "";
			installPath = "";
			thumbnailDir = "";
			thumbDatPath = "";
		}
	}
}
